<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 
    xmlns:tax="http://di.uminho.pt/taxonomy#">
        <xsl:template match="/">
                <rdf:RDF
                                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
                                xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
                                xmlns:tax="http://di.uminho.pt/taxonomy#">
                    
                 <xsl:apply-templates mode="node" select="//node"/>
                 </rdf:RDF>
        </xsl:template>
    
    
    
    <xsl:template mode="node" match="node" >
        <xsl:choose>
            <xsl:when test="@id">
                <rdf:Description rdf:nodeID="{@id}">
                    <xsl:if test="@label">
                        <tax:label><xsl:value-of select="@label"/></tax:label>
                    </xsl:if>
                    <xsl:if test="isComposedBy">
                        <tax:isComposedBy>
                            <rdf:Bag>
                                <xsl:for-each select="./isComposedBy/node">
                                        <xsl:choose>
                                            <xsl:when test="@id">
                                                <rdf:li rdf:resource="{@id}"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <rdf:li rdf:resource="{generate-id()}"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                </xsl:for-each>
                            </rdf:Bag>
                        </tax:isComposedBy>
                    </xsl:if>
                    <xsl:if test="isRelatedTo">
                        <tax:isRelatedTo>
                            <xsl:for-each select="./isRelatedTo/node">
                                    <xsl:choose>
                                        <xsl:when test="@id">
                                            <rdf:li rdf:resource="{@id}"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <rdf:li rdf:resource="{generate-id()}"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                            </xsl:for-each>
                        </tax:isRelatedTo>
                    </xsl:if>
                    <xsl:if test ="hasNote">
                        <xsl:choose>
                            <xsl:when test="./hasNote/@type">
                                <tax:hasNote type="{./hasNote/@type}"><xsl:value-of select="hasNote"/></tax:hasNote>
                            </xsl:when>
                            <xsl:otherwise>
                                <tax:hasNote><xsl:value-of select="hasNote"/></tax:hasNote>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:if>
                </rdf:Description>
            </xsl:when>
            <xsl:otherwise>
                <rdf:Description rdf:nodeID="{generate-id()}">
                    <xsl:if test="@label">
                    <tax:label><xsl:value-of select="@label"/></tax:label>
                    </xsl:if>
                    <xsl:if test="isComposedBy">
                    <tax:isComposedBy>
                        <rdf:Bag>
                          <xsl:for-each select="./isComposedBy/node">
                                      <xsl:choose>
                                          <xsl:when test="@id">
                                              <rdf:li rdf:resource="{@id}"/>
                                          </xsl:when>
                                          <xsl:otherwise>
                                              <rdf:li rdf:resource="{generate-id()}"/>
                                          </xsl:otherwise>
                                      </xsl:choose>
                          </xsl:for-each>
                        </rdf:Bag>
                    </tax:isComposedBy>
                    </xsl:if>
                    <xsl:if test="isRelatedTo">
                    <tax:isRelatedTo>
                        <xsl:for-each select="./isRelatedTo/node">
                                <xsl:choose>
                                    <xsl:when test="@id">
                                        <rdf:li rdf:resource="{@id}"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <rdf:li rdf:resource="{generate-id()}"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                        </xsl:for-each>
                    </tax:isRelatedTo>
                    </xsl:if>
                    <xsl:if test ="hasNote">
                        <xsl:choose>
                            <xsl:when test="./hasNote/@type">
                                <tax:hasNote type="{./hasNote/@type}"><xsl:value-of select="hasNote"/></tax:hasNote>
                            </xsl:when>
                            <xsl:otherwise>
                                <tax:hasNote><xsl:value-of select="hasNote"/></tax:hasNote>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:if>
                </rdf:Description>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
